package com.marvels.approve.process.listener;

import java.util.Map;

import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;

import com.marvels.approve.manager.dto.ActivitiNodeDTO;
import com.marvels.approve.process.dto.CompleteProcessDTO;
import com.marvels.approve.process.dto.ProcessApproveDTO;
import com.marvels.approve.process.service.IProcessService;
import com.marvels.common.constants.CaseNodeConstant;
import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.MarvelsLogUtil;

/**
 * 线上
 * @author tangpw
 *
 */
public class OnlineProductsListen implements ExecutionListener{
	
	
	@Autowired
	private TaskService taskService;

	@Autowired
	private IProcessService processService;
	
	
	public boolean runningDecision(String loanId){
		return true;
	}
	
	@Override
	public void notify(DelegateExecution execution) throws Exception {
		MarvelsLogUtil.getInstance().info(">>>>>>>>>>线上产品征信报告审核监听开始");
//		String processInstanceId = execution.getProcessInstanceId();
//		Object object = execution.getVariable(processInstanceId);
//		String loanId = "";
//		if (null != object) {
//			Map<String, Object> map = (Map<String, Object>) object;
//			loanId = map.get("loanId") + "";
//		}
//		// 跑决策
//		boolean isPass=false;
//		try {
//			isPass = runningDecision(loanId);
//		} catch (Exception e) {
//			MarvelsLogUtil.getInstance().error(">>>>>>>loanId" + loanId + ",线上产品征信报告审核监听:", e);
//		}
//		TaskQuery tq = taskService.createTaskQuery().processInstanceId(processInstanceId);
//		Task task = tq.singleResult();
//		String curNodeCode = task.getTaskDefinitionKey();
//		String curNodeName = task.getName();
//		String curTaskId = task.getId();
//		CompleteProcessDTO proDto = new CompleteProcessDTO();
//		MarvelsLogUtil.getInstance().info(">>>>>>>>>>>>>线上产品征信报告审核监听curNodeCode:" + curNodeCode + ",curNodeName"
//				+ curNodeName + ",curTaskId" + curTaskId);
//		// 决策通过 征信审核 
//		ProcessApproveDTO processApproveDto = new ProcessApproveDTO();
//		if (isPass) {
//			MarvelsLogUtil.getInstance().error(">>>>>>>loanId" + loanId + ",线上产品征信报告审核决策通过");
//			ActivitiNodeDTO curNode = new ActivitiNodeDTO();
//			processApproveDto.setProcessResult(MarvelsConstant.PROCESS_RESULT_OK);
//			// 设置当前节点
//			proDto.setCurNode(curNode);
//		} else {// 决策拒绝 结案
//			MarvelsLogUtil.getInstance().error(">>>>>>>loanId" + loanId + ",线上产品征信报告审核决策拒绝");
//			ActivitiNodeDTO curNode = new ActivitiNodeDTO();
//			processApproveDto.setProcessResult(MarvelsConstant.PROCESS_RESULT_REFUSE);
//			// 设置当前节点
//			proDto.setCurNode(curNode);
//		}
//		try {
//			proDto.setProcessApproveDto(processApproveDto);
//			processService.completeLoanApplyCase(proDto);
//		} catch (MarvelsBusinessException e) {
//			MarvelsLogUtil.getInstance().error(">>>>>>>loanId" + loanId + ",线上产品征信报告审核监听异常:", e);
//		}
//		
		
	}

}
